﻿using System.Collections.ObjectModel;
using System.Reflection;
using MeterReader.Common.Helpers;
using MeterReader.Models;

namespace MeterReader.Common.Extensions
{
    /// <summary>
    /// 讲表对象转换成集合
    /// </summary>
    public static class ServableCollectionExtensions
    {
        public static void AddNewMeterDataCollection(this ObservableCollection<MeterDataCollection> oMeterData, TableMeters tMeters)
        {
            // 获取 T_Meters 中的所有属性,查询所有 double 属性
            var propertyInfos = tMeters
                .GetType()
                .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(p => p.Name.StartsWith("Col") && int.TryParse(p.Name.Substring(3), out _) && p.PropertyType == typeof(double) && p.CanRead)
                .OrderBy(p => int.Parse(p.Name.Substring(3)));

            int pCount = propertyInfos.Count();
            int oCount = oMeterData.Count;

            if (oCount == pCount)
            {
                // 更新数据
                AddToCollection(propertyInfos, tMeters, oMeterData);
            }
            else
            {
                // 初始化，创建对象
                InitCollection(propertyInfos, tMeters, oMeterData);
            }
        }

        private static void InitCollection(
            IEnumerable<PropertyInfo> propertyInfos,
            TableMeters tMeters,
            ObservableCollection<MeterDataCollection> oMeterData
        )
        {
            var validProperties = propertyInfos.Select((p, index) => new { PropertyInfo = p, Index = index + 1 });

            foreach (var item in validProperties)
            {
                var meter = new MeterData
                {
                    MeterName = $"电表：{item.Index}",
                    MeterValue = (double)item.PropertyInfo.GetValue(tMeters, null)!,
                    MeterUpdateTime = tMeters.Date,
                    MeterStatus = MeterStatus.Fault
                };

                if (meter.MeterValue == 0)
                {
                    meter.MeterStatus = MeterStatus.Fault;
                }
                else
                {
                    meter.MeterStatus = MeterStatus.Working;
                }

                var meterMeasurements = new MeterDataCollection();
                meterMeasurements.AddToCollection(meter);
                oMeterData.Add(meterMeasurements);
            }
        }

        private static void AddToCollection(
            IEnumerable<PropertyInfo> propertyInfos,
            TableMeters tMeters,
            ObservableCollection<MeterDataCollection> oMeterData
        )
        {
            var validProperties = propertyInfos.Select((p, index) => new { PropertyInfo = p, Index = index + 1 });

            foreach (var p in validProperties)
            {
                var meter = new MeterData
                {
                    MeterName = $"电表：{p.Index}",
                    MeterValue = (double)p.PropertyInfo.GetValue(tMeters, null)!,
                    MeterUpdateTime = tMeters.Date,
                    MeterStatus = MeterStatus.Fault
                };

                if (meter.MeterValue == 0)
                {
                    meter.MeterStatus = MeterStatus.Fault;
                }
                else
                {
                    meter.MeterStatus = MeterStatus.Working;
                }
                oMeterData[p.Index - 1].AddToCollection(meter);
            }
        }
    }
}
